package top.kaoshanji.p2ch19a;

import top.kaoshanji.p2ch19a.support.Tourist;

import java.util.concurrent.CyclicBarrier;

/**
 * 代码清单-p2ch19a-533页a：CyclicBarrier应用示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/28 10:15
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) {

        int num = 3;
        Tourist[] threads = new Tourist[num];

        // 共享的协作变量
        CyclicBarrier barrier = new CyclicBarrier(num, new Runnable() {
            @Override
            public void run() {
                System.out.println("all arrived " + System.currentTimeMillis() + " executed by " + Thread.currentThread().getName());
            }
        });

        for (int i = 0; i < num; i++) {
            threads[i] = new Tourist(barrier);
            threads[i].start();
        }


    }


}
